package database;

import java.sql.ResultSet;

import main.Terminal;
import model.Discount;
import model.Sale;

/**
 * 
 * @author Anders
 *
 */
public class DBDiscount
{
	private final String ID = "ID";
	//private final String TYPE = "Type";
	//private final String AMOUNT = "Amount";
	//private final String SALE_ID = "SaleID";
	
	//private final String selectAll = String.format("SELECT %s, %s, %s, %s FROM Discount", ID, TYPE, AMOUNT, SALE_ID);
	
	private DBConnection con;
	
	public DBDiscount()
	{
		con = DBConnection.getInstance();
	}
	
	public boolean addDiscount(Discount discount)
	{
		boolean result = true;
		if(discount.getSale() != null) {
			int id = discount.getId();
			String type = discount.getType();
			double amount = discount.getAmount();
			Sale sale = discount.getSale();
			
			con.execute(String.format("INSERT INTO Discount VALUES('%d', '%s', '%d', '%d')", id, type, (int) amount, sale.getId()));
		}
		else {
			result = false;
		}
		
		return result;
	}
	
	public int getMaxId()
	{
	
		int id = -1; //"error" code	TODO: change error code?
		try {
			String query = String.format("SELECT MAX(%s) AS ID FROM Discount", ID);
			ResultSet resSet = con.executeResSet(query);
			resSet.next();
			id = resSet.getInt("ID");
		}
		catch (Exception e) {
			Terminal.say("Error finding max id", e);
		}
		return id;		
	}
	
//	private String selectWhere(String condition)
//	{
//		return selectAll + "WHERE " + condition;
//	}
}
